using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;
using log4net;

namespace Sedna.Core.Controls
{
	public class AddressTextField : BaseControl, INamingContainer, IClientScriptingControl
	{		

//		private ILog log=LogManager.GetLogger(typeof(AddressTextField));

		private TextBox street1TextBox = new TextBox("street1TextBox");
		private TextBox street2TextBox = new TextBox("street2TextBox");
		private TextBox street3TextBox = new TextBox("street3TextBox");
		private TextBox cityTextBox = new TextBox("cityTextBox");
		private TextBox stateTextBox = new TextBox("stateTextBox");
		private TextBox zipTextBox = new TextBox("zipTextBox");

		public override bool ClientScriptingEnabled
		{
			get { return true; }
			set { /*Always enabled;*/ }
		}

		public string Street1
		{
			get { return street1TextBox.Text; }
			set { street1TextBox.Text=value; }
		}

	    public string Street2
	    {
	        get { return street2TextBox.Text; }
	        set { street2TextBox.Text = value; }
	    }

	    public string Street3
	    {
	        get { return street3TextBox.Text; }
	        set { street3TextBox.Text = value; }
	    }


	    public string City
	    {
	        get { return cityTextBox.Text; }
	        set { cityTextBox.Text = value; }
	    }

	    public string State
	    {
	        get { return stateTextBox.Text; }
	        set { stateTextBox.Text = value; }
	    }

	    public string Zip
	    {
	        get { return zipTextBox.Text; }
	        set { zipTextBox.Text = value; }
	    }

        public override bool Enabled
        {
            get { return base.Enabled; }
            set
            {
                EnsureChildControls();
                base.Enabled = value;
                street1TextBox.Enabled = value;
                street2TextBox.Enabled = value;
                street3TextBox.Enabled = value;
                cityTextBox.Enabled = value;
                stateTextBox.Enabled = value;
                cityTextBox.Enabled = value;
                zipTextBox.Enabled = value;
            }
        }

		public AddressTextField() : base()
		{
			configurationId = "configuration.default.AddressTextField";
		}

		public AddressTextField(IConfiguration configuration): base(configuration)
		{
		}

	    private TableCell BuildCell(params Control[] controls)
	    {
	        TableCell cell = new TableCell();
	        foreach (Control control in controls)
	        {
	            cell.Controls.Add(control);
	        }
            return cell;
	    }
	    
	    private TableRow BuildRow(params TableCell[] cells)
	    {
	        TableRow row = new TableRow();
	        foreach (TableCell cell in cells)
	        {
	            row.Controls.Add(cell);
	        }
            return row;
	    }
	    
		protected override void CreateChildControls()
		{
		    Table table = new Table();
            table.BorderStyle = BorderStyle.None;
            table.CellSpacing = 0;
            table.CellPadding = 0;
            table.Width = Unit.Percentage(100);

			//IE has a bug: <INPUT> always has extra 1px margin on the bottom & on the top,
			//so we use 1px margin instead of 3px
			street1TextBox.Style.Add("margin-bottom", "1px");
			street1TextBox.Width = Unit.Percentage(100);
            TableCell cell = BuildCell(street1TextBox);
            cell.ColumnSpan = 3;
            table.Rows.Add(BuildRow(cell));
		    
			street2TextBox.Width = Unit.Percentage(100);
			street2TextBox.Style.Add("margin-bottom", "1px");
            cell = BuildCell(street2TextBox);
            cell.ColumnSpan = 3;
            table.Rows.Add(BuildRow(cell));

			street3TextBox.Width = Unit.Percentage(100);
			street3TextBox.Style.Add("margin-bottom", "1px");
            cell = BuildCell(street3TextBox);
            cell.ColumnSpan = 3;
            table.Rows.Add(BuildRow(cell));

            TableRow row = new TableRow();
		    
			cityTextBox.Width = Unit.Percentage(100);
            cell = BuildCell(cityTextBox);
		    cell.Width = Unit.Percentage(33);
            row.Cells.Add(cell);

            stateTextBox.Width = Unit.Percentage(100);
			stateTextBox.Style.Add("margin-left", "3px");
			stateTextBox.Style.Add("margin-right", "3px");
            cell = BuildCell(stateTextBox);
            cell.Width = Unit.Percentage(33);
            row.Cells.Add(cell);

            zipTextBox.Width = Unit.Percentage(100);
            cell = BuildCell(zipTextBox);
            cell.Width = Unit.Percentage(33);
            row.Cells.Add(cell);

            table.Rows.Add(row);
		    
		    Controls.Add(table);
		}

	    protected override void OnPreRender(EventArgs e)
	    {
	        base.OnPreRender(e);
	        if (ClientScriptingEnabled)
	        {
	            DeclareScriptVariable();   
	        }
	        
	    }

	    protected override void DeclareScriptVariable()
	    {
			ControlUtil.EnsureIdAttribute(street1TextBox);
			ControlUtil.EnsureIdAttribute(street2TextBox);
			ControlUtil.EnsureIdAttribute(street3TextBox);
			ControlUtil.EnsureIdAttribute(cityTextBox);
			ControlUtil.EnsureIdAttribute(stateTextBox);
			ControlUtil.EnsureIdAttribute(zipTextBox);
            base.DeclareScriptVariable();
	    }

	    protected override IDictionary BuildScriptParameters()
	    {
	        IDictionary parameters =  base.BuildScriptParameters();
            parameters["street1Id"] = street1TextBox.ClientID;
            parameters["street2Id"] = street2TextBox.ClientID;
            parameters["street3Id"] = street3TextBox.ClientID;
            parameters["cityId"] = cityTextBox.ClientID;
            parameters["stateId"] = stateTextBox.ClientID;
            parameters["zipId"] = zipTextBox.ClientID;
            parameters["submitState"] = "submitState="+new ScriptObjectGenerator(GetSubmitState()).ToScript();
            return parameters;
	    }

	    private IDictionary GetSubmitState()
		{
			IDictionary state=new Hashtable();
			state["street1"]=Street1;
			state["street2"]=Street2;
			state["street3"]=Street3;
			state["city"]=City;
			state["state"]=State;
			state["zip"]=Zip;
			return state;
		}

	
		protected override void Bind()
		{
			binder.Bind(new string[]{"Street1", "Street2", "Street3", "City", "State", "Zip"});
		}

		protected override void Unbind()
		{
			binder.Unbind(new string[]{"Street1", "Street2", "Street3", "City", "State", "Zip"});	
		}

	}

}
